home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / vol7n16.arc / QSORTI.C < prev    next >
Text File  |  1988-09-27  |  1KB  |  38 lines

  1. void quicksort(int left, int right)
  2. {
  3.     int i, j, t;                    /* some scratch variables */
  4.  
  5.     if(right > left)                /* skip unnecessary calls */
  6.     {   
  7.         i = left-1; j = right;      /* initialize scan pointers */
  8.  
  9.                                     /* partition array on value */
  10.                                     /* of the rightmost item */
  11.         do {
  12.                                     /* scan right for item >=  */
  13.                                     /* than partitioning value */
  14.             do i++; 
  15.                 while(items[i] < items[right]);
  16.  
  17.                                     /* scan left for item <=   */
  18.                                     /* than partitioning value */
  19.             do j--; 
  20.                 while(items[j] > items[right] && j > 0); 
  21.  
  22.             t = items[i];           /* interchange the items */
  23.             items[i] = items[j];
  24.             items[j] = t;
  25.  
  26.         } while(j > i);             /* do until pointers cross */
  27.  
  28.         items[j] = items[i];        /* undo the last swap and */
  29.         items[i] = items[right];    /* put the partitioning */
  30.         items[right] = t;           /* element into position */
  31.  
  32.         quicksort(left, i-1);       /* sort items to left of */
  33.                                     /* partitioning element */
  34.  
  35.         quicksort(i+1, right);      /* sort items to right of */
  36.     }                               /* partitioning element */
  37. }
  38.